package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.infrastructure.persistent.mapper.HolidayMapper;
import org.dxy.infrastructure.persistent.po.HolidayPo;
import org.dxy.infrastructure.persistent.service.HolidayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 假期数据服务实现
 * 
 * @author dxy
 * @createTime 2025/9/21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HolidayServiceImpl extends ServiceImpl<HolidayMapper, HolidayPo> implements HolidayService {
    

    @Override
    public List<HolidayPo> getByDateRange(LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<HolidayPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(HolidayPo::getDate, startDate, endDate)
                   .orderByAsc(HolidayPo::getDate);
        return this.list(queryWrapper);
    }
    
    @Override
    public HolidayPo getByDate(LocalDate date) {
        LambdaQueryWrapper<HolidayPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HolidayPo::getDate, date);
        return this.getOne(queryWrapper);
    }
    

    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByDate(LocalDate date) {
        LambdaQueryWrapper<HolidayPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HolidayPo::getDate, date);
        this.remove(queryWrapper);
    }
    
    @Override
    public List<HolidayPo> getByYear(Integer year) {
        LocalDate startDate = LocalDate.of(year, 1, 1);
        LocalDate endDate = LocalDate.of(year, 12, 31);
        return getByDateRange(startDate, endDate);
    }
}